$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'pinkao/pinkaocourse/list',
        datatype: "json",
        colModel: [			
			{ label: 'pinKaoCourseId', name: 'pinKaoCourseId', index: 'pin_kao_course_id', width: 50, key: true,hidden:true},
			// { label: '拼考课程名称', name: 'courseName', index: 'course_name', width: 80 },
			{ label: '标题', name: 'title', index: 'title', width: 80 },
			// { label: '拼考内容的图片', name: 'courseImage', index: 'course_image', width: 80 },
			{ label: '内容', name: 'courseContent', index: 'course_content', width: 80 },
            { label: '封面', name: 'coverImgs', index: 'cover_imgs', width: 80,
                formatter: function (data) {
            	let imgs=data.split(";");
            	let html=''
					for(let i=0;i<imgs.length;i++){
            			html=html+'<img src="'+imgs[i]+'" style="height: 40px;width: 40px"></img>';
					}

                    return html;

                }   },
			{ label: '原价', name: 'oldMoney', index: 'old_money', width: 80 }, 			
			{ label: '现价', name: 'nowMoney', index: 'now_money', width: 80 },
            { label: '类别', name: 'classification', index: 'classification', width: 80 },
            { label: '已拼数', name: 'pinKaoCount', index: 'pin_kao_count', width: 80 },
			{ label: '开始时间', name: 'startTime', index: 'start_time', width: 80 },
			{ label: '结束时间', name: 'endTime', index: 'end_time', width: 80 },
			{ label: '状态', name: 'state', index: 'state', width: 80,
                formatter: function (data,row) {
                    var html;
                    if(data==0){
                        html=` <button class="btn btn-primary btn-xs" onclick="online(${row.rowId})"></i>&nbsp;未上线</button>`
                    }else {
                        html=` <button class="btn btn-success btn-xs"onclick="upline(${row.rowId})"></i>&nbsp;已上线</button>`

                    }
                    return html;

                } },
            { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
            { label: '操作', name: 'pinKaoCourseId', index: 'cover_imgs', width: 80,
                formatter: function (data,row) {
                    return '<input type="button" class="btn btn-primary btn-xs" onclick="showYulan('+row.rowId+')" value="预览"/>'+
                        '<input type="button" class="btn btn-success btn-xs" onclick="showPinkao('+row.rowId+')" value="详情"/>';

                }   }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });





});
function showPinkao(id) {
	vm.pinKaoCourseId=id
	vm.showList=1
	vm.getInfo(id)
    var colunms = Menu.initColumn();
    var table = new TreeTable(Menu.id, baseURL + "pinkao/pinkaocourse/getPinkaoTeamList?pinKaoId="+id, colunms);
    table.setExpandColumn(0);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentId");
    table.setExpandAll(false);
    table.init();
    Menu.table = table;
}
function showYulan(id) {
    vm.pinKaoCourseId=id
    vm.showList=2
    vm.getInfo(id)
}



function online(id) {
    onlineWindow(id,1,"确认要上线么？")
}
function upline(id){
    onlineWindow(id,0,"确认要下架么？")
}

function onlineWindow(id,isOnline,msg){
    var data={
        pinKaoCourseId:id,
        state:isOnline
    }
    confirm(msg, function(){
        $.ajax({
            type: "POST",
            url: baseURL + "pinkao/pinkaocourse/online",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function(r){
                vm.reload()
                console.log(r)
                alert("操作成功")
            }
        });
    });
}


var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: 0,
		title: null,
        pinKaoCourseId:null,
		pinKaoCourse: {}
	},
	methods: {
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.pinKaoCourse = {};
		},
		update: function (event) {
			var pinKaoCourseId = getSelectedRow();
			if(pinKaoCourseId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(pinKaoCourseId)
		},
		saveOrUpdate: function (event) {
			var url = vm.pinKaoCourse.pinKaoCourseId == null ? "pinkao/pinkaocourse/save" : "pinkao/pinkaocourse/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.pinKaoCourse),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var pinKaoCourseIds = getSelectedRows();
			if(pinKaoCourseIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "pinkao/pinkaocourse/delete",
                    contentType: "application/json",
				    data: JSON.stringify(pinKaoCourseIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(pinKaoCourseId){
			$.get(baseURL + "pinkao/pinkaocourse/info/"+pinKaoCourseId, function(r){
                vm.pinKaoCourse = r.pinKaoCourse;
            });
		},
		reload: function (event) {
			vm.showList = 0;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});

var Menu = {
    id: "menuTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Menu.initColumn = function () {
    var columns = [
        // {field: 'selectItem', radio: true},
        //{title: '菜单ID', field: 'menuId', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '团', field: 'nickNames', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '总人数', field: 'pinKaoTeamCount', align: 'center', valign: 'middle', sortable: true, width: '160px'},
        {title: '头像', field: 'icon', align: 'center', valign: 'middle', sortable: true, width: '80px', formatter: function(item, index){
                return item.icon == null ? '' : '<i class="'+item.icon+' fa-lg"></i>';
            }},
        {title: '昵称', field: 'pinKaoNickName', align: 'center', valign: 'middle', sortable: true, width: '160px'},
        {title: '手机', field: 'mobile', align: 'center', valign: 'middle', sortable: true, width: '160px'},
        {title: '是否完结', field: 'isSuccess', align: 'center', valign: 'middle', sortable: true, width: '160px', formatter: function(item, index){
                        if(item.isSuccess == 1)
                            return '未拼完'
                       else if(item.isSuccess == 0)
                            return '已完结'
                        else
                            return ''
            }},
        ]
    return columns;
};